/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.postorder.flow.refund;

import cn.hutool.core.util.ObjectUtil;
import com.lxk.oms.order.service.IOmsOrderService;
import com.lxk.oms.postorder.domain.enums.PostOrderEnums;
import com.lxk.oms.postorder.domain.req.AuditPostOrderReq;
import com.lxk.oms.postorder.domain.vo.OmsPostOrderItemVo;
import com.lxk.oms.postorder.domain.vo.OmsPostOrderVo;
import com.lxk.oms.postorder.flow.audit.PoAuditContext;
import com.lxk.oms.postorder.service.IOmsPostOrderItemService;
import com.lxk.oms.postorder.service.IOmsPostOrderService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.log.LFLog;
import com.yomahub.liteflow.log.LFLoggerManager;
import lombok.AllArgsConstructor;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.ValidationUtils;
import org.dromara.common.core.validate.ContextGroup;

import java.util.List;

import static com.lxk.oms.postorder.flow.refund.FlowRefundPostOrderNodeConst.REFUND_COMMENT_FILL_POST_ORDER_NODE;

/**
 * 仅退款处理
 *
 * @author heyi
 * 2024/9/29
 */
@AllArgsConstructor
@LiteflowComponent(value = REFUND_COMMENT_FILL_POST_ORDER_NODE)
public class PostOrderFillNode extends NodeComponent {
    private final IOmsPostOrderService postOrderService;
    private final IOmsPostOrderItemService postOrderItemService;
    private final IOmsOrderService omsOrderService;
    private final LFLog log = LFLoggerManager.getLogger(FlowExecutor.class);

    @Override
    public void beforeProcess() {
        AuditPostOrderReq req = this.getRequestData();
        ValidationUtils.validate(req);
        PoAuditContext contextBean = this.getContextBean(PoAuditContext.class);
        MapstructUtils.convert(req, contextBean);
    }

    @Override
    public void process() throws Exception {
        PoAuditContext contextBean = this.getContextBean(PoAuditContext.class);
        String postOrderNo = contextBean.getPostOrderNo();
        OmsPostOrderVo omsPostOrderVo = postOrderService.queryByNo(postOrderNo, PostOrderEnums.PostOrderType.REFUND_RETURN.getPostOrderType());
        if (ObjectUtil.isNull(omsPostOrderVo)) {
            log.error("售后订单已处理:{}", postOrderNo);
            throw new ServiceException("售后订单已处理");
        }
        List<OmsPostOrderItemVo> omsPostOrderItemVos = postOrderItemService.queryPostOrderId(omsPostOrderVo.getId());

        contextBean.setPostOrderItemVoList(omsPostOrderItemVos);
        contextBean.setPostOrderVo(omsPostOrderVo);
        ValidationUtils.validate(contextBean, ContextGroup.class);
    }
}
